<?php

namespace app\api\controller;

use ba\Tree;
use think\facade\Db;
use think\facade\Config;
use think\facade\Log;
use app\common\controller\Frontend;

class Index extends Frontend
{
    protected $noNeedLogin = ['index','getPhone','baojing'];

    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->auth->isLogin()) {
            $rule = $this->auth->getMenus();

            // 首页加载的规则，验权，但过滤掉会员中心菜单
            foreach ($rule as $key => $item) {
                if (in_array($item['type'], ['menu_dir', 'menu'])) unset($rule[$key]);
            }
            $rule = array_values($rule);
        } else {
            $rule = Db::name('user_rule')
                ->where('status', '1')
                ->where('no_login_valid', 1)
                ->where('type', 'in', ['route', 'nav', 'button'])
                ->order('weigh', 'desc')
                ->select()
                ->toArray();
            $rule = Tree::instance()->assembleChild($rule);
        }

        $this->success('', [
            'site'             => [
                'siteName'     => get_sys_config('site_name'),
                'recordNumber' => get_sys_config('record_number'),
                'version'      => get_sys_config('version'),
                'cdnUrl'       => full_url(),
                'upload'       => get_upload_config(),
            ],
            'openMemberCenter' => Config::get('buildadmin.open_member_center'),
            'rules'            => $rule
        ]);
    }




    public function getPhone()
    {
        $param = $this->request->get();
        file_put_contents('/www/wwwroot/dzgyapi.jiantaokeji.com/public/log/jie_dian_hua.txt',json_encode($param)."\n",FILE_APPEND);
        if (isset($param['remote_number'])) {
            $baoJingQiIp = $param['remote_number'];
            $sosInfo = Db::name('park_talk_sos')->where('ip',$baoJingQiIp)->find();
            Db::name('park_talk_sos_log')->insert([
                'sos_id'=>$sosInfo['id'],
                'createtime'=>date('Y-m-d H:i:s')
            ]);
        }
        //file_put_contents('/www/wwwroot/dzgyapi.jiantaokeji.com/public/log/phoneData.txt',json_encode($param)."\n",FILE_APPEND);
        echo 'success';
    }

    //摁报警按钮触发事件推送--消息接收地址
    public function baojing()
    {
        $param = $this->request->get();
        //file_put_contents('/www/wwwroot/dzgyapi.jiantaokeji.com/public/log/phoneData.txt',json_encode($param)."\n",FILE_APPEND);
        file_put_contents('/www/wwwroot/dzgyapi.jiantaokeji.com/public/log/an_bao_jing.txt',json_encode($param)."\n",FILE_APPEND);
    }
}